package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.kgc.sbt.api.FavoriteApi;
import com.kgc.sbt.config.annotation.RequestPermission;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * Created on 2021/6/23 0023.
 * <p>
 * Author: px
 * <p>
 * Description: 个人收藏操作入口
 */
@RestController
@Api(tags = "个人收藏操作入口，版本号：1.0.0")
@Slf4j
@RequestPermission
public class FavoriteController {

    @Reference
    private FavoriteApi favoriteApi;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * @Author px
     * @Date 2021/6/23 0023
     * @Param [userId]
     * @Description: 根据用户id查询收藏列表
     **/
    /*@GetMapping("/queryFavoriteById")
    @ApiOperation(value = "根据用户id查询收藏列表", notes = " ")
    public RequestResult<List> queryFavoriteById(@RequestParam Integer userId){
        return ResultBuildUtil.success(favoriteApi.queryFavoriteByUserId(userId));
    }*/



    /**
     * @Author px
     * @Date 2021/6/23 0023
     * @Param
     * @Description: 添加收藏
     **/
    @GetMapping("/addFavorite")
    @ApiOperation(value = "添加收藏", notes = "不能频繁点击")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",required = true,value = "商品id")
    })
    public RequestResult<String> addFavoriteById(@RequestParam Integer goodsId, HttpServletRequest request){

        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer) redisUtils.get(token);
        log.info("------ userId:" + userId);

        StringBuffer sbf = new StringBuffer();
        sbf.append("goodsId=").append(goodsId).append("userId=").append(userId);

        if(redisUtils.checkFreq(sbf.toString(),5,60)){
            if (favoriteApi.addFavoriteById(goodsId, userId)){
                return ResultBuildUtil.success("加入收藏成功!");
            }
            return ResultBuildUtil.fail("1111", "该商品已加入收藏..请勿重复操作..");
        }
        /*redisUtils.checkFreq(sbf.toString(),5,60);

        if (favoriteApi.addFavoriteById(goodsId, userId)){
            return ResultBuildUtil.success("加入收藏成功!");
        }
        return ResultBuildUtil.fail("2333", "该商品已加入收藏..请勿重复操作..");*/
        return ResultBuildUtil.fail("9999", "请勿重复点击");

    }



    /**
     * @Author px
     * @Date 2021/6/23 0023
     * @Param
     * @Description: 移出收藏
     **/
    @GetMapping("/delFavorite")
    @ApiOperation(value = "移出收藏", notes = "不能频繁点击")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId",required = true,value = "商品id")
    })
    public RequestResult<String> delFavoriteById(@RequestParam Integer goodsId, HttpServletRequest request){

        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer) redisUtils.get(token);
        log.info("------ userId:" + userId);

        StringBuffer sbf = new StringBuffer();
        sbf.append("goodsId=").append(goodsId).append("userId=").append(userId);

        if(redisUtils.checkFreq(sbf.toString(),5,60)){
            if (favoriteApi.delFavoriteById(goodsId, userId)){
                return ResultBuildUtil.success("移出收藏成功!");
            }
            return ResultBuildUtil.fail("1111", "该商品已移出收藏..请勿重复操作....");
        }
        /*if (favoriteApi.delFavoriteById(goodsId, userId)){
            return ResultBuildUtil.success("移出收藏成功!");
        }
        return ResultBuildUtil.fail("2333", "该商品已移出收藏..请勿重复操作..");
*/
        return ResultBuildUtil.fail("9999", "请勿重复点击");
    }







}




